package com.njxzc.demo.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njxzc.demo.common.http.ResponseResult;
import com.njxzc.demo.entity.CartEntity;
import com.njxzc.demo.entity.GoodsEntity;
import com.njxzc.demo.entity.OrderdetailEntity;
import com.njxzc.demo.entity.OrdersEntity;
import com.njxzc.demo.mapper.admin.OrdersMapper;
import com.njxzc.demo.service.before.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, OrdersEntity> implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private CartService cartService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OrderdetailService orderdetailService;

    @Override
    public ResponseResult<Map<String, Object>> getAllOrders(OrdersEntity ordersEntity) {
        if(ordersEntity.getCurrentPage() == null || ordersEntity.getCurrentPage() < 1)
            ordersEntity.setCurrentPage(1);
        IPage<OrdersEntity> iPage = new Page<>(ordersEntity.getCurrentPage(), ordersEntity.getPageSize());
        LambdaQueryWrapper<OrdersEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if(ordersEntity.getId() != null){
            lambdaQueryWrapper.eq(OrdersEntity::getId, ordersEntity.getId());
        }

        IPage<OrdersEntity> page = page(iPage, lambdaQueryWrapper);

        Map<String ,Object> myres = new HashMap<>();
        myres.put("allOrders",page.getRecords());
        myres.put("totalPage",page.getPages());
        return ResponseResult.getSuccessResult(myres);
    }

    @Override
    public ResponseResult<List<Map<String, Object>>> getOrdersDetail(OrdersEntity ordersEntity) {
        return ResponseResult.getSuccessResult(ordersMapper.getOrdersDetail(ordersEntity.getId()));
    }

    @Override
    public ResponseResult<OrdersEntity> submitOrder(OrdersEntity ordersEntity) {
        ordersEntity.setStatus(0);
        ordersEntity.setOrderdate(new Date());
        save(ordersEntity);

        //生成详情
        List<Integer> bgid = ordersEntity.getBgid();
        List<Integer> bshoppingnum = ordersEntity.getBshoppingnum();
        ArrayList<OrderdetailEntity> orderdetailEntities = new ArrayList<OrderdetailEntity>();
        ArrayList<GoodsEntity> ges= new ArrayList<GoodsEntity>();
        for(int i=0; i<bgid.size(); i++){
            OrderdetailEntity ort = new OrderdetailEntity();
            GoodsEntity ge = new GoodsEntity();
            ort.setOrderbasetableId(ordersEntity.getId());
            ort.setGoodstableId(bgid.get(i));
            ort.setShoppingnum(bshoppingnum.get(i));
            orderdetailEntities.add(ort);
            //减少库存
            ge.setId(bgid.get(i));
            ge.setGstore(goodsService.getById(bgid.get(i)).getGstore() - bshoppingnum.get(i));
            ges.add(ge);
        }
        orderdetailService.saveBatch(orderdetailEntities);
        goodsService.updateBatchById(ges);

        //清空购物车
        CartEntity entity = new CartEntity();
        entity.setGoodstableId(ordersEntity.getBusertableId());
        cartService.clearCart(entity);

        return ResponseResult.getSuccessResult(ordersEntity);
    }

    @Override
    public ResponseResult<OrdersEntity> goSubmitOrder(OrdersEntity ordersEntity) {
        ordersEntity.setStatus(0);
        ordersEntity.setOrderdate(new Date());
        save(ordersEntity);

        //生成详情
        List<Integer> bgid = ordersEntity.getBgid();
        List<Integer> bshoppingnum = ordersEntity.getBshoppingnum();
        ArrayList<OrderdetailEntity> orderdetailEntities = new ArrayList<OrderdetailEntity>();
        ArrayList<GoodsEntity> ges= new ArrayList<GoodsEntity>();
        for(int i=0; i<bgid.size(); i++){
            OrderdetailEntity ort = new OrderdetailEntity();
            GoodsEntity ge = new GoodsEntity();
            ort.setOrderbasetableId(ordersEntity.getId());
            ort.setGoodstableId(bgid.get(i));
            ort.setShoppingnum(bshoppingnum.get(i));
            orderdetailEntities.add(ort);
            //减少库存
            ge.setId(bgid.get(i));
            ge.setGstore(goodsService.getById(bgid.get(i)).getGstore() - bshoppingnum.get(i));
            ges.add(ge);
        }
        orderdetailService.saveBatch(orderdetailEntities);
        goodsService.updateBatchById(ges);

        return ResponseResult.getSuccessResult(ordersEntity);
    }

    @Override
    public ResponseResult<Map<String, Object>> getOrderByUid(OrdersEntity ordersEntity) {
        Page<OrdersEntity> ipage = new Page<>(ordersEntity.getCurrentPage(), 5);
        LambdaQueryWrapper<OrdersEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OrdersEntity::getBusertableId, ordersEntity.getBusertableId());
        Page<OrdersEntity> page = page(ipage, lqw);

        Map<String, Object> myres = new HashMap<>();
        myres.put("ordersByUid", page.getRecords());
        myres.put("totalPage", page.getPages());

        return ResponseResult.getSuccessResult(myres);
    }
}
